package com.im.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


/** 
 * 采用MD5加密解密 
 * @author tfq 
 * @datetime 2011-10-13 
 */  
public class MD5Util {  
	
	private static final Logger logger = LogManager.getLogger(MD5Util.class);
  
    /*** 
     * MD5加码 生成32位md5码 
     */  
	public static String string2MD5(String inStr,String Type,Long instId){
		//登陆密码加密
		//账户查询密码
		//支付密码
		//提现密码
		if(instId==null){
			instId = 0L;
		}
		if(Domain.PW_TYPE.get("login").equals(Type)){
			inStr = Domain.CUST_PWKEY+ inStr + instId.toString();
		}else if(Domain.PW_TYPE.get("acctQry").equals(Type)){
			
		}else if(Domain.PW_TYPE.get("payment").equals(Type)){
			
		}else if(Domain.PW_TYPE.get("withdraw").equals(Type)){
			
		}
		return MD5(inStr);
	}
	
    public static String MD5(String inStr){
        MessageDigest md5 = null;  
        try{  
            md5 = MessageDigest.getInstance("MD5");  
        }catch (Exception e){  
            logger.error(e.getMessage());
            return "";  
        }  
        char[] charArray = inStr.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  
            byteArray[i] = (byte) charArray[i];  
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++){  
            int val = ((int) md5Bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
  
    }

    public synchronized static final String getMD5Str(String str,String charSet) { //md5加密  
        MessageDigest messageDigest = null;    
        try {    
            messageDigest = MessageDigest.getInstance("MD5");    
            messageDigest.reset();   
            if(charSet==null){  
                messageDigest.update(str.getBytes());  
            }else{  
                messageDigest.update(str.getBytes(charSet));    
            }             
        } catch (NoSuchAlgorithmException e) {    
              
        } catch (UnsupportedEncodingException e) {    
             
        }    
          
        byte[] byteArray = messageDigest.digest();    
        StringBuffer md5StrBuff = new StringBuffer();    
        for (int i = 0; i < byteArray.length; i++) {                
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)    
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));    
            else    
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));    
        }    
        return md5StrBuff.toString();    
    }  
  
    /** 
     * 加密解密算法 执行一次加密，两次解密 
     */   
    public static String convertMD5(String inStr){  
  
        char[] a = inStr.toCharArray();  
        for (int i = 0; i < a.length; i++){  
            a[i] = (char) (a[i] ^ 't');  
        }  
        String s = new String(a);  
        return s;  
  
    }
    
    public static void main(String [] args){
    	MD5Util util=new MD5Util();
    	String md5Str=string2MD5("123456","1",10749l);
    	System.out.println(md5Str);
    	System.out.println("100200001:"+MD5Util.MD5("100200001"));
    	System.out.println("100300001:"+MD5Util.MD5("100300001"));
    	System.out.println("100400001:"+MD5Util.MD5("100400001"));
    	System.out.println("100500001:"+MD5Util.MD5("100500001"));
    	System.out.println("100600001:"+MD5Util.MD5("100600001"));
    	//System.out.println(util.getMD5Str("Chuangjia000000_VruXJNNDYdrog_10749", "utf-8"));
    }
}  